package com.xinghai.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghai.common.annotation.DataScope;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.system.domain.XhContractInfo;
import com.xinghai.system.domain.XhDriverInfo;
import com.xinghai.system.domain.XhVehicleOrderCarHis;
import com.xinghai.system.mapper.XhContractInfoMapper;
import com.xinghai.system.service.IXhContractInfoService;
import com.xinghai.system.service.IXhVehicleOrderCarHisService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 合同信息管理Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-01
 */
@Service
public class XhContractInfoServiceImpl extends ServiceImpl<XhContractInfoMapper, XhContractInfo> implements IXhContractInfoService
{
    @Autowired
    private XhContractInfoMapper xhContractInfoMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private IXhVehicleOrderCarHisService xhVehicleOrderCarHisService;
    /**
     * 查询合同信息管理
     * 
     * @param id 合同信息管理主键
     * @return 合同信息管理
     */
    @Override
    public XhContractInfo getInfo(Long id)
    {
        return xhContractInfoMapper.getInfo(id);
    }

    /**
     * 查询合同信息管理列表
     * 
     * @param xhContractInfo 合同信息管理
     * @return 合同信息管理
     */
    @Override
    @DataScope(deptAlias = "c")
    public List<XhContractInfo> list(XhContractInfo xhContractInfo)
    {
        xhContractInfo.setDelFlag("0");
        List<XhContractInfo> list = xhContractInfoMapper.list(xhContractInfo);
        list.forEach(item->{
            //从用车历史中查询临时司机信息
            XhVehicleOrderCarHis orderCarHis = xhVehicleOrderCarHisService.lambdaQuery()
                    .eq(XhVehicleOrderCarHis::getVehicleId,item.getVehicleId())
                    .orderByDesc(XhVehicleOrderCarHis::getCreateTime)
                    .last(" limit 1").one();
            if (orderCarHis != null && StringUtils.isNotNull(orderCarHis.getAppendDriverInfo())){
                String appendDriverName = String.join(",", JSON.parseArray(orderCarHis.getAppendDriverInfo(), XhDriverInfo.class).stream().map(XhDriverInfo::getDriverName).collect(Collectors.toList()));
                item.setDriverName(StringUtils.isNotEmpty(item.getDriverName())?(item.getDriverName() + "," + appendDriverName):appendDriverName);
            }
        });
        return list;
    }

    /**
     * 新增合同信息管理
     * 
     * @param xhContractInfo 合同信息管理
     * @return 结果
     */
    @Override
    public int insert(XhContractInfo xhContractInfo)
    {
        xhContractInfo.setCreateTime(DateUtils.getNowDate());
        return xhContractInfoMapper.insert(xhContractInfo);
    }
    /**
     * 批量新增合同信息管理
     *
     * @param xhContractInfoList 合同信息管理
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhContractInfo> xhContractInfoList)
    {
        return xhContractInfoMapper.insertBatch(xhContractInfoList);
    }
    /**
     * 修改合同信息管理
     * 
     * @param xhContractInfo 合同信息管理
     * @return 结果
     */
    @Override
    public int update(XhContractInfo xhContractInfo)
    {
        xhContractInfo.setUpdateTime(DateUtils.getNowDate());
        return xhContractInfoMapper.update(xhContractInfo);
    }

    /**
     * 批量删除合同信息管理
     * 
     * @param ids 需要删除的合同信息管理主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return xhContractInfoMapper.deleteBatch(ids);
    }

    /**
     * 删除合同信息管理信息
     * 
     * @param id 合同信息管理主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return xhContractInfoMapper.delete(id);
    }

    /**
     * 导入合同信息管理信息
     *
     * @param list 合同信息管理信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhContractInfo> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhContractInfoMapper xhContractInfoMapper = sqlSession.getMapper(XhContractInfoMapper.class);
            list.stream().forEach(item -> xhContractInfoMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
